package ai.taromed.vr.menu;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import ai.taromed.vr.DimenUtils;
import ai.taromed.vr.R;


public class FilterMenu implements IMenu {

    private List<Item> items = new ArrayList<>();

    private TextView lastClickMenuItem = null;
    private OnMenuChangeListener listener;
    private FilterMenuLayout layout;
//    /**
//     * add menu item to layout
//     *
//     * @param item
//     * @param listener
//     */
//    public void addItem(Item item, View.OnClickListener listener) {
//        items.add(item);
//    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

    public OnMenuChangeListener getListener() {
        return listener;
    }

    public void setListener(OnMenuChangeListener listener) {
        this.listener = listener;
        for (final Item item : getItems()) {
            item.getView().setOnClickListener(v -> {
                Log.e("FilterMenu", "onClick");
                if (getListener() != null) {
                    Log.e("FilterMenu", "-----item.getPosition():" + item.getPosition());
                    changeMenuItemBg(item.getView());
                    getListener().onMenuItemClick(item.getView(), item.getPosition());
                }
                if (layout != null) {
                    layout.collapse(true);
                }
            });
        }
    }

    public void changeMenuItemBg(int position){
        Item item = getItems().get(position);
        this.changeMenuItemBg(item.getView());
    }

    private void changeMenuItemBg(View currentMenuItem){
        if(lastClickMenuItem != null){
            lastClickMenuItem.setTextColor(Color.WHITE);
            lastClickMenuItem.setBackgroundColor(Color.TRANSPARENT);
            lastClickMenuItem.setBackground(null);
        }
        if(currentMenuItem instanceof TextView){
            TextView currentTextView = (TextView)currentMenuItem;
            currentTextView.setTextColor(Color.WHITE);
            currentTextView.setBackground(currentMenuItem.getContext().getResources().getDrawable(R.drawable.menu_selected_round_bg , null));
//            currentTextView.setBackgroundColor(Color.argb(200 , 27, 51 , 133));
            this.lastClickMenuItem = currentTextView;
        }
        this.layout.requestLayout();

    }

    @Override
    public void collapse(boolean animate) {
        layout.collapse(animate);
    }

    @Override
    public void expand(boolean animate) {
        layout.expand(animate);
    }

    @Override
    public void toggle(boolean animate) {
        layout.toggle(animate);
    }

    @Override
    public void setMenuLayout(FilterMenuLayout view) {
        this.layout = view;
        if (view == null) {
            return;
        }
        for (final Item item : getItems()) {
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT , ViewGroup.LayoutParams.WRAP_CONTENT);
            layout.addView(item.getView() , lp);
        }
        layout.setMenu(this);


        // 初始化条目的样式
        initMenuItemStyle();
    }

    private void initMenuItemStyle(){
        for (Item item : getItems()) {
            View menu = item.getView();
            menu.setFocusable(true);
            menu.setOnFocusChangeListener((v, hasFocus) -> {
                if(hasFocus){
                    v.setBackground(v.getContext().getResources().getDrawable(R.drawable.menu_focused_round_bg , null));
                }else{
                    if(lastClickMenuItem != null && lastClickMenuItem == menu){
                        v.setBackground(v.getContext().getResources().getDrawable(R.drawable.menu_focused_round_bg , null));
                    }else{
                        v.setBackground(null);
                        v.setBackgroundColor(Color.TRANSPARENT);
                    }
                }
            });
        }

    }










    public static interface OnMenuChangeListener {
        void onMenuItemClick(View view, int position);

        void onMenuCollapse();

        void onMenuExpand();
    }

    public static class Builder {
        OnMenuChangeListener listener;
        private List<Item> items = new ArrayList<>();
        private Context ctx;
        private LayoutInflater inflater;
        private FilterMenuLayout layout;

        public Builder(Context ctx) {
            this.ctx = ctx;
            this.inflater = LayoutInflater.from(ctx);
        }

        public Builder withListener(OnMenuChangeListener listener) {
            this.listener = listener;
            return this;
        }

        public Builder inflate(int menuResId) {
            PopupMenu popupMenu = new PopupMenu(ctx, null);
            popupMenu.inflate(menuResId);
            Menu menu = popupMenu.getMenu();
            for (int i = 0; i < menu.size(); i++) {
                MenuItem item = menu.getItem(i);
                addItem(item.getIcon());
            }
            menu.clear();
            menu = null;
            popupMenu = null;
            return this;
        }

        public Builder addItem(Drawable icon) {
            ImageButton view = (ImageButton) inflater.inflate(R.layout.menu_item, null, false);
            view.setImageDrawable(icon);
//            TypedValue value = new TypedValue();
//            ctx.getTheme().resolveAttribute(R.attr.selectableItemBackgroundBorderless, value, true);
//            view.setBackgroundResource(value.resourceId);
            addItem(view);
            return this;
        }

        public Builder addItem(int titleResId) {
            TextView menu = new TextView(inflater.getContext());
            menu.setText(ctx.getResources().getString(titleResId));
            menu.setTextSize(TypedValue.COMPLEX_UNIT_SP , 12);
            menu.setTextColor(Color.WHITE);
            menu.setPadding(DimenUtils.dp2px(ctx , 18),
                    DimenUtils.dp2px(ctx , 12),
                    DimenUtils.dp2px(ctx , 0),
                    DimenUtils.dp2px(ctx , 12));
            addItem(menu);
            return this;
        }

        /**
         * 添加底部的返回按钮
         * @param backView
         * @return
         */
        public Builder addBackItem(View backView){
            Item item = new Item();
            item.setView(backView);
            item.setPosition(items.size());
            item.getView().setTag(item);
            item.setBottom(true);
            items.add(item);
            return this;
        }

        public Builder addItem(View customView) {
            Item item = new Item();
            item.setView(customView);
            item.setPosition(items.size());
            item.getView().setTag(item);
            items.add(item);

            return this;
        }

        public Builder attach(FilterMenuLayout view) {
            this.layout = view;
            return this;
        }

        public FilterMenu build() {
            FilterMenu menu = new FilterMenu();
            menu.setItems(items);
            menu.setListener(this.listener);
            menu.setMenuLayout(this.layout);
            return menu;
        }
    }

    public static class Item {
        private View view;
        private int x;
        private int y;
        private int position;
        private boolean isBottom = false;
        private Rect bounds = new Rect(0, 0, 0, 0);

        public View getView() {
            return view;
        }

        public void setView(View view) {
            this.view = view;
            view.setAlpha(0f);
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }

        public int getPosition() {
            return position;
        }

        public void setPosition(int position) {
            this.position = position;
        }

        public void setBounds(int left, int top, int right, int bottom) {
            this.bounds.set(left, top, right, bottom);
        }

        public Rect getBounds() {
            return bounds;
        }

        public void setBounds(Rect bounds) {
            this.bounds = bounds;
        }

        public boolean isBottom() {
            return isBottom;
        }

        public void setBottom(boolean bottom) {
            isBottom = bottom;
        }

    }
}
